package comwork.example.halou.dog_live_tvt;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.widget.RadioGroup;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import comwork.example.halou.dog_live_tvt.fragments.AnchorFragment;
import comwork.example.halou.dog_live_tvt.fragments.ClassficationFragment;
import comwork.example.halou.dog_live_tvt.fragments.HomeFragment;
import comwork.example.halou.dog_live_tvt.fragments.MineFragment;

public class MainActivity extends AppCompatActivity {
    //我是主要的版本
    private RadioGroup rg_comtiner;
    private List<Fragment> dataFragment=new ArrayList<>();
//    记录要隐藏的碎片


    private int currentIndex = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        rg_comtiner.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rg_home:
                        selectFragment(0);
                        break;
                    case R.id.rg_classification:
                        selectFragment(1);
                        break;
                    case R.id.rg_Anchor:
                        selectFragment(2);
                        break;
                    case R.id.rg_mine:
                        selectFragment(3);
                        break;
                }
            }
        });
//        隐藏actionbar
        ActionBar supportActionBar = getSupportActionBar();
        if (supportActionBar.isShowing()) {
            supportActionBar.hide();
        }
    }

    private void selectFragment(int targetIndex) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        Fragment targetfragment = dataFragment.get(targetIndex);
        Fragment currentfragment = dataFragment.get(currentIndex);
        if (!targetfragment.isAdded()) {
            transaction.add(R.id.commen_continer,targetfragment).hide(currentfragment).commit();
        }else {
            transaction.show(targetfragment).hide(currentfragment).commit();
        }
        currentIndex=targetIndex;


    }

    private void initView() {
        rg_comtiner = (RadioGroup) findViewById(R.id.continer);
        dataFragment.add(new HomeFragment());
        dataFragment.add(new ClassficationFragment());
        dataFragment.add(new AnchorFragment());
        dataFragment.add(new MineFragment());
//先将首页的碎片给显示出来
        getSupportFragmentManager().beginTransaction().add(R.id.commen_continer,dataFragment.get(0)).commit();
    }
    private long mExiTime;

    //    点击退出的按钮的实现
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (keyCode== KeyEvent.KEYCODE_BACK&&event.getRepeatCount()==0) {
            exit();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
    public void exit(){
//        点击的时间超过两秒就会 让你再点击一次然后才能退出
        if ((System.currentTimeMillis() - mExiTime) > 2000) {
            Toast.makeText(MainActivity.this, "再按一次退出", Toast.LENGTH_SHORT).show();
            mExiTime = System.currentTimeMillis();
        } else {
//       如果点击的时间没有超过两秒就直接进行退出
            finish();
            System.exit(0);
        }
    }
}
